
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public TreeNode constructFromPrePost(int[] preorder, int[] postorder) {
        int N=preorder.length;
        if(N==0){
            return null;
        }
        TreeNode root=new TreeNode(preorder[0]);
        if(N==1){
            return root;
        }
        int L=0;
        for(int i=0;i<N;i++){
            if(postorder[i]==preorder[1]){
                L=i+1;
            }
        }
        root.left=constructFromPrePost(Arrays.copyOfRange(preorder,1,L+1),
                Arrays.copyOfRange(postorder,0,L));
        root.right=constructFromPrePost(Arrays.copyOfRange(preorder,L+1,N),
                Arrays.copyOfRange(postorder,L,N-1));
        return root;


    }
}